from fastapi import APIRouter, Query
from tortoise.expressions import Q
from app.controllers.mini.settings import mini_settings_controller
from app.schemas.base import Success, SuccessExtra
from app.schemas.mini import *
from app.settings.config import settings

mini_settings_router = APIRouter()
model_name = "配置"


@mini_settings_router.get("/list", summary=f"查看{model_name}列表")
async def do_settings_list(
        page: int = Query(1, description="页码"),
        page_size: int = Query(10, description="每页数量"),
        name: str = Query(None, description=f"{model_name}名称"),
):
    q = Q()
    if name:
        q &= Q(name__contains=name)
    total, data = await mini_settings_controller.do_list(page=page, page_size=page_size, search=q)
    return SuccessExtra(data=data, total=total, page=page, page_size=page_size)

@mini_settings_router.get("/get", summary=f"查看{model_name}")
async def do_settings_get(
        data_id: int = Query(..., description="ID"),
):
    data = await mini_settings_controller.do_detail(data_id=data_id)
    return Success(data=data)

@mini_settings_router.post("/create", summary=f"创建{model_name}")
async def do_settings_create(
        data_in: MiniSettingsCreate,
):
    await mini_settings_controller.do_create(obj_in=data_in)
    return Success(msg="创建成功")

@mini_settings_router.post("/update", summary=f"更新{model_name}")
async def do_settings_update(
        data_in: MiniSettingsUpdate,
):
    await mini_settings_controller.do_update(obj_in=data_in)
    return Success(msg="更新成功")

@mini_settings_router.delete("/delete", summary=f"删除{model_name}")
async def do_settings_delete(
        data_id: int = Query(..., description="ID"),
):
    await mini_settings_controller.do_delete(data_id=data_id)
    return Success(msg="删除成功")

@mini_settings_router.get("/go_type", summary="获取跳转类型")
async def list_level():
    level_objs = settings.GO_TYPE
    data = []
    key = 0
    for level in level_objs:
        if level:
            data.append({"label":level,"value":key})
        key += 1
    return Success(data=data)
